home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / shape.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-13  |  2.9 KB  |  116 lines

  1. //
  2. // "$Id: shape.cxx,v 1.5 1999/01/13 15:45:50 mike Exp $"
  3. //
  4. // Tiny OpenGL demo program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <config.h>
  27. #include <FL/Fl.H>
  28. #include <FL/Fl_Window.H>
  29. #include <FL/Fl_Hor_Slider.H>
  30. #include <FL/math.h>
  31.  
  32. #if HAVE_GL
  33.  
  34. #include <FL/gl.h>
  35. #include <FL/Fl_Gl_Window.H>
  36.  
  37. class shape_window : public Fl_Gl_Window {
  38.   void draw();
  39. public:
  40.   int sides;
  41.   shape_window(int x,int y,int w,int h,const char *l=0);
  42. };
  43.  
  44. shape_window::shape_window(int x,int y,int w,int h,const char *l) :
  45. Fl_Gl_Window(x,y,w,h,l) {
  46.   sides = 3;
  47. }
  48.  
  49. void shape_window::draw() {
  50. // the valid() property may be used to avoid reinitializing your
  51. // GL transformation for each redraw:
  52.   if (!valid()) {
  53.     valid(1);
  54.     glLoadIdentity();
  55.     glViewport(0,0,w(),h());
  56.   }
  57. // draw an amazing graphic:
  58.   glClear(GL_COLOR_BUFFER_BIT);
  59.   glColor3f(.5,.6,.7);
  60.   glBegin(GL_POLYGON);
  61.   for (int i=0; i<sides; i++) {
  62.     double ang = i*2*M_PI/sides;
  63.     glVertex3f(cos(ang),sin(ang),0);
  64.   }
  65.   glEnd();
  66. }
  67.  
  68. #else
  69.  
  70. #include <FL/Fl_Box.H>
  71. class shape_window : public Fl_Box {
  72. public:    
  73.   int sides;
  74.   shape_window(int x,int y,int w,int h,const char *l=0)
  75.     :Fl_Box(FL_DOWN_BOX,x,y,w,h,l){
  76.       label("This demo does\nnot work without GL");
  77.   }
  78. };
  79.  
  80. #endif
  81.  
  82. // when you change the data, as in this callback, you must call redraw():
  83. void sides_cb(Fl_Widget *o, void *p) {
  84.   shape_window *sw = (shape_window *)p;
  85.   sw->sides = int(((Fl_Slider *)o)->value());
  86.   sw->redraw();
  87. }
  88.  
  89. int main(int argc, char **argv) {
  90.  
  91.   Fl_Window window(300, 330);
  92.  
  93. // the shape window could be it's own window, but here we make it
  94. // a child window:
  95.   shape_window sw(10, 10, 280, 280);
  96. // make it resize:
  97.   window.resizable(&sw);
  98.   //  window.size_range(300,330,0,0,1,1,1);
  99. // add a knob to control it:
  100.   Fl_Hor_Slider slider(50, 295, window.w()-60, 30, "Sides:");
  101.   slider.align(FL_ALIGN_LEFT);
  102.   slider.callback(sides_cb,&sw);
  103.   slider.value(sw.sides);
  104.   slider.step(1);
  105.   slider.bounds(3,40);
  106.  
  107.   window.end();
  108.   window.show(argc,argv);
  109.     
  110.   return Fl::run();
  111. }
  112.  
  113. //
  114. // End of "$Id: shape.cxx,v 1.5 1999/01/13 15:45:50 mike Exp $".
  115. //
  116.